<template>
  <div>
    <!--搜索表单-->
    <div style="display: flex;margin-bottom: 10px">
      <div style="width: 200px">
        <el-popconfirm title="你确定删除所选选课信息吗？" @confirm="deleteSelected">
          <el-button slot="reference" type="danger" style="margin-left: 5px">删除所选选课</el-button>
        </el-popconfirm>
      </div>
      <div style="flex: 1;text-align: right">
        <el-input v-model="params.sno" clearable placeholder="请输入学号"
                  style="width: 240px;margin-left: 5px"></el-input>
        <el-input v-model="params.cno" clearable placeholder="请输入课程号"
                  style="width: 240px;margin-left: 5px"></el-input>
        <el-input v-model="params.tno" clearable placeholder="请输入教工号"
                  style="width: 240px;margin-left: 5px"></el-input>
        <el-button class="el-icon-search" type="primary" style="margin-left: 5px" @click="load">搜索</el-button>
        <el-button class="el-icon-refresh" type="primary" style="margin-left: 5px" @click="reset">重置</el-button>
      </div>
    </div>

    <!--主体-->
    <el-table :data="tableData" ref="multipleTable" @selection-change="handleSelectionChange" :cell-style="{'text-align':'center'}" :header-cell-style="{'text-align':'center'}"
              stripe>
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column prop="sno" label="学号"></el-table-column>
      <el-table-column prop="sname" label="学生姓名"></el-table-column>
      <el-table-column prop="cno" label="课程号"></el-table-column>
      <el-table-column prop="cname" label="课程名"></el-table-column>
      <el-table-column prop="tno" label="教工号"></el-table-column>
      <el-table-column prop="tname" label="教师姓名"></el-table-column>
      <el-table-column prop="grade" label="成绩"></el-table-column>
      <el-table-column label="操作">
        <template v-slot="scope">
          <!--scope.row就是当前行数据-->
          <el-popconfirm title="你确定删除该选课信息吗？" @confirm="del(scope.row.sno,scope.row.cno,scope.row.tno)">
            <el-button slot="reference" type="danger" style="margin-left: 5px">删除</el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <!--分页-->
    <div style="margin-top: 10px">
      <el-pagination
          background
          :current-page="params.pageNum"
          :page-size="params.pageSize"
          @current-change="handleCurrentChange"
          layout="prev, pager, next"
          :total="total">
      </el-pagination>
    </div>
  </div>
</template>

<script>

import request1 from "@/utils/request1";

export default {
  name: "Department",
  data() {
    return {
      dialogVisibleforadd: false,
      dialogVisibleforedit: false,
      total: 0,
      params: {
        pageNum: 1,
        pageSize: 10,
        sno: '',
        cno: '',
        tno: ''
      },
      form: {},
      tableData: [],
      multipleSelection: []
    }
  },
  created() {
    this.load()
  },
  methods: {
    load() {
      request1.get('/sct/page', {params: this.params}).then(res => {
        if (res.code === '200') {
          this.tableData = res.data.list
          this.total = res.data.total
        }
      })
    },
    reset() {
      this.params = {
        pageNum: 1,
        pageSize: 10,
        sno: '',
        cno: '',
        tno: ''
      }
      this.load()
    },
    handleCurrentChange(pageNum) {
      this.params.pageNum = pageNum
      this.load()
    },
    del(sno, cno, tno) {
      request1.delete('/sct/delete/' + sno + '/' + cno + '/' + tno).then(res => {
        if (res.code === '200') {
          this.$notify.success('删除成功')
          this.load()
        } else {
          this.$notify.error(res.msg)
        }
      })
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    deleteSelected() {
      if (this.multipleSelection.length === 0) {
        this.$notify.success('请勾选要删除的选项')
      } else {
        request1.post('/sct/deleteSelected', this.multipleSelection).then(res => {
          if (res.code === '200') {
            this.$notify.success('删除成功')
            this.load()
          } else {
            this.$notify.success('删除失败')
          }
        })
      }
    }
  }
}
</script>
